package org.example.carsystem.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.carsystem.domain.SysUser;
import org.example.carsystem.domain.dto.SysUserDTO;
import org.example.carsystem.domain.dto.SysUserExportDTO;
import org.example.carsystem.service.UserService;
import org.example.carsystem.utils.ExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户条件查询
     * */
    @GetMapping("/select")
    public Map<String,Object> usersSelect(@RequestParam("deptId")Long deptId,
                                          @RequestParam(value = "userName",required = false)String userName,
                                          @RequestParam(value = "phoneNumber",required = false)String phoneNumber,
                                          @RequestParam(value = "status",required = false)String status,
                                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createStartTime,
                                          @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createEndTime,
                                          @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize){
        System.out.println(pageNum+""+pageSize);
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SysUser> info = new PageInfo<>(userService.usersSelect(deptId,userName,phoneNumber,status,createStartTime,createEndTime));
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("list",info);
        return map;
    }
    /**
     * 用户添加
     * */
    @PostMapping("/add")
    public Map<String,Object> usersAdd(@RequestBody SysUserDTO sysUserDTO){
        userService.usersAdd(sysUserDTO);
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","添加成功");
        return map;
    }

    @GetMapping("select/by/id")
    public Map<String,Object> SelectById(@RequestParam("userId")Long userId){
        System.out.println(userId);
        SysUserDTO CarUser = userService.SelectById(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("data",CarUser);
        return map;
    }

    @PostMapping("/update")
    public Map<String,Object> updateUser(@RequestBody SysUserDTO sysUserDTO){
        userService.updateUser(sysUserDTO);
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","修改成功");
        return map;
    }

    @GetMapping("/update/status")
    public Map<String,Object> updateUserStatus(@RequestParam("userId")Long userId,@RequestParam("status")String status){
        userService.updateUserStatus(userId,status);
        Map<String, Object> map = new HashMap<>();
        map.put("coded",200);
        map.put("message","更改成功");
        return map;
    }
    @DeleteMapping("/del")
    public Map<String,Object> DeleteUser(@RequestParam("userId")Long[] userId){
        userService.DeleteUser(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","删除成功");
        return map;
    }

    @GetMapping("/export")
    public void exportUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String param = request.getParameter("params");//参数信息
        SysUserExportDTO sysUserExportDTO = JSONUtil.toBean(param, SysUserExportDTO.class);
        System.err.println(sysUserExportDTO);
        List<SysUser> sysUserList = userService.usersSelect(sysUserExportDTO.getDeptId(), sysUserExportDTO.getUserName(), sysUserExportDTO.getPhoneNumber(), sysUserExportDTO.getStatus(), sysUserExportDTO.getCreateStartTime(), sysUserExportDTO.getCreateEndTime());
        ExportUtils.export(response, "用户信息", "用户sheet", sysUserList);
    }

    @PostMapping("/import")
    public void importUsers(MultipartFile file) throws IOException {
        userService.importUsers(file);
    }

    @RequestMapping("/doLoad")
    public void doLoad(HttpServletResponse response){
        String name = "用户导入模版";
        String url = "CarParent/CarSystem/src/main/resources/static/file/用户导入模板.xlsx";
        ExportUtils.doLoadTemplate(response,name,url);
    }
}
